<?php

namespace App\Http\Transformers\User;

use App\Models\UserLoginLog;
use League\Fractal\TransformerAbstract;
use App\Models\User;

class UserPageTransformer extends TransformerAbstract
{
    public function transform(User $transform)
    {
        $data = $transform->toArray();

        //积分和余额
        data_set($data, 'amount', data_get($data, 'total_amount.amount', 0));
        data_set($data, 'integralAmount', data_get($data, 'total_integral.integralAmount', 0));

        //下下级数量
        $fanCount = 0;
        if($fan = data_get($data, 'fan_num')){
            foreach ($fan as $value){
                $fanCount += $value['fanNums'];
            }
        }

        //下级数量
        $count = data_get($data, 'fanNums', 0) + $fanCount;

        //下级及下下级的数量
        data_set($data, 'fanNum', $count);

        //实名认证
        data_set($data, 'real_status', data_get($data, 'authentication.real_status', 0));
        unset($data['authentication']);

        return $data;
    }
}
